package com.shuoyue.ycgk.ui.questionbank.practice.freedom;

import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.FreedomGroup;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.ui.papergroups.special.SpecialAdapter;
import com.shuoyue.ycgk.ui.questionbank.practice.QuestionSpecialCountAdapter;
import com.shuoyue.ycgk.utils.GridSpacingItemDecoration;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnItemClick;

public class FreedomCountAdapter extends AppBaseQuickAdapter<FreedomGroup> {
    OnItemChildClickListener listener;

    public FreedomCountAdapter(@Nullable  List<FreedomGroup> data, OnItemChildClickListener listener) {
        super(R.layout.item_freedom_count, data);
        this.listener = listener;
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(BaseViewHolder helper, FreedomGroup item) {
        helper.setText(R.id.name, item.getDetailName());
        RecyclerView recyclerView = helper.getView(R.id.childRecycleView);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
        if (recyclerView.getTag() == null) {
            recyclerView.addItemDecoration(new GridSpacingItemDecoration(5, SizeUtil.dip2px(mContext, 20), false));
            recyclerView.setTag("tag");
        }
        QuestionSpecialCountAdapter specialAdapter = new QuestionSpecialCountAdapter(allSimple(item.getQuestionDTOS()));
        recyclerView.setAdapter(specialAdapter);
        specialAdapter.setOnItemChildClickListener(listener);
    }


    /**
     * 提取基础题目
     */
    private List<QuestionSimple> allSimple(List<QuestionParent> questionParents) {
        List<QuestionSimple> allSimple = new ArrayList<>();
        for (QuestionParent questionParent : questionParents) {
            //
            if (questionParent.getIsMaterial() == 1) {
                //材料题
                for (QuestionSimple questionSimple : questionParent.getQuestionSimpleDTOS()) {
                    allSimple.add(questionSimple);
                }
            } else {
                allSimple.add(questionParent.getQuestionSimpleDTOS().get(0));
            }
        }
        return allSimple;
    }
}
